package com.ruoyi.lyy.service.impl;

import com.ruoyi.lyy.domain.dto.UpdateOrderDTO;
import com.ruoyi.lyy.domain.pojo.Order;
import com.ruoyi.lyy.domain.vo.OrderVO;
import com.ruoyi.lyy.mapper.OrderMapper;
import com.ruoyi.lyy.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author YueYang
 * @since 2025-11-18
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    private final OrderMapper orderMapper;

    /**
     * 更新订单状态
     *
     * @param id             订单id
     * @param updateOrderDTO 更新订单状态DTO
     * @return 影响行数
     */
    @Override
    public int updateOrderById(Integer id, UpdateOrderDTO updateOrderDTO) {
        Order order = new Order();
        order.setId(id);
        //下单时间
        order.setOrderTime(LocalDateTime.now());
        //判断核销码图片是否有值
        if (updateOrderDTO.getVerifyCodeImg() != null) {
            order.setVerificationImg(updateOrderDTO.getVerifyCodeImg());
        }
        order.setUpdateBy("admin");
        order.setUpdateTime(LocalDateTime.now());
        //修改订单的支付状态
        order.setPayStatus(updateOrderDTO.getPayStatus());
        //修改订单的状态
        order.setStatus(updateOrderDTO.getOrderStatus());
        order.setPayTime(LocalDateTime.now());

        log.info("订单id为{}的订单状态更新成功", id);
        //更新订单
        return orderMapper.updateById(order);
    }

    /**
     * 根据订单id查询订单信息
     *
     * @param id 订单id
     * @return 订单信息
     */
    @Override
    public OrderVO getOrderById(Integer id) {
        return orderMapper.queryOrderById(id);
    }
}
